﻿using System.Collections.Generic;
using System.Linq;
using BOL.Models.Genome;

namespace BOL.Apps.GeneNetworkBrowser.Controls
{
    public partial class FilterPane
    {
        #region Private variables

        private TissueTypes _tissueTypes;
        private HumanChromosomeTypes _humanChromosomeTypes;
        private RelationTypes _relationTypes;

        private Network _network;

        #endregion

        #region Public properties

        public Network Network { get { return _network; } set { _network = value; } }

        public IEnumerable<Gene> SelectedGenes
        {
            get
            {
                var genes = _network.Vertices;
                var relations = _network.Edges;

                // Tissue types
                if (_tissueFilter._cbTissueAll.IsChecked != null && _tissueFilter._cbTissueAll.IsChecked.Value)
                {
                    _tissueTypes = _tissueFilter.TissueTypes;

                    genes = genes.Where(x => x.TissueTypes.HasFlag(_tissueTypes)).ToList();
                }

                // Human chromosome types
                if (_humanChromosomeFilter._cbChrAll.IsChecked != null && _humanChromosomeFilter._cbChrAll.IsChecked.Value)
                {
                    _humanChromosomeTypes = _humanChromosomeFilter.HumanChromosomeTypes;
                    var chromosomes = new HashSet<Chromosome>();

                    if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr1))
                        chromosomes.Add(new Chromosome(9606, "Chr1"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr2))
                        chromosomes.Add(new Chromosome(9606, "Chr2"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr3))
                        chromosomes.Add(new Chromosome(9606, "Chr3"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr4))
                        chromosomes.Add(new Chromosome(9606, "Chr4"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr5))
                        chromosomes.Add(new Chromosome(9606, "Chr5"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr6))
                        chromosomes.Add(new Chromosome(9606, "Chr6"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr7))
                        chromosomes.Add(new Chromosome(9606, "Chr7"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr8))
                        chromosomes.Add(new Chromosome(9606, "Chr8"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr9))
                        chromosomes.Add(new Chromosome(9606, "Chr9"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr10))
                        chromosomes.Add(new Chromosome(9606, "Chr10"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr11))
                        chromosomes.Add(new Chromosome(9606, "Chr11"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr12))
                        chromosomes.Add(new Chromosome(9606, "Chr12"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr13))
                        chromosomes.Add(new Chromosome(9606, "Chr13"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr14))
                        chromosomes.Add(new Chromosome(9606, "Chr14"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr15))
                        chromosomes.Add(new Chromosome(9606, "Chr15"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr16))
                        chromosomes.Add(new Chromosome(9606, "Chr16"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr17))
                        chromosomes.Add(new Chromosome(9606, "Chr17"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr18))
                        chromosomes.Add(new Chromosome(9606, "Chr18"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr19))
                        chromosomes.Add(new Chromosome(9606, "Chr19"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr20))
                        chromosomes.Add(new Chromosome(9606, "Chr20"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr21))
                        chromosomes.Add(new Chromosome(9606, "Chr21"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.Chr22))
                        chromosomes.Add(new Chromosome(9606, "Chr22"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.ChrX))
                        chromosomes.Add(new Chromosome(9606, "ChrX"));
                    else if (_humanChromosomeTypes.HasFlag(HumanChromosomeTypes.ChrY))
                        chromosomes.Add(new Chromosome(9606, "ChrY"));

                    genes = genes.Where(x => chromosomes.Contains(x.Chromosome)).ToList();
                }

                // Relation types
                if (_relationTypeFilter._cbDatabaseAll.IsChecked != null && _relationTypeFilter._cbDatabaseAll.IsChecked.Value)
                {
                    _relationTypes = _relationTypeFilter.RelationTypes;

                    relations = relations.Where(x => x.ContainsFlag(_relationTypes) && genes.Contains(x.Source) && genes.Contains(x.Target));
                    var sources = relations.Select(x => x.Source);
                    var targets = relations.Select(x => x.Target);
                    
                    genes = sources.Union(targets).Distinct();
                }

                // Score
                //if (_sliderScore.Value != 1)
                //{
                //    relations = relations.Where(x => x.ContainsFlag(_relationTypes) && genes.Contains(x.Source) && genes.Contains(x.Target));
                    
                //}

                return genes;
            }
        }

        #endregion

        #region Constructor

        public FilterPane()
        {
            InitializeComponent();
        }

        #endregion
    }
}
